package com.itheima.Controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Bean.Category;
import com.itheima.Bean.Dish;
import com.itheima.Bean.Setmeal;
import com.itheima.Bean.SetmealDish;
import com.itheima.Service.CategoryService;
import com.itheima.Service.SetmealDishService;
import com.itheima.Service.SetmealService;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/status/{status}")
    public R<String> setStatus(@PathVariable Integer status, @RequestParam List<Long> ids){
        LambdaUpdateWrapper<Setmeal> luw = new LambdaUpdateWrapper<>();
        luw.in(Setmeal::getId,ids);
        luw.set(Setmeal::getStatus,status);
        setmealService.update(luw);
        return R.success("修改状态成功！");
    }


    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改套餐成功！！");
    }

    @PostMapping
    public R<String> saveWithDishes(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDishes(setmealDto);
        return R.success("新增套餐成功！");
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> page(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    String name){
        Page<SetmealDto> setmealDtoPage = setmealService.findPage(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    public R<String> deleteWithDish(@RequestParam List<Long> ids){
        setmealService.deleteWithDish(ids);
        return R.success("删除套餐成功！");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus,setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);
        return R.success(setmealService.list(lqw));
    }

    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){

        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //给setmealDto赋值categoryName
        LambdaQueryWrapper<Category> clqw = new LambdaQueryWrapper<>();
        clqw.eq(Category::getId,setmeal.getCategoryId());
        setmealDto.setCategoryName(categoryService.getOne(clqw).getName());

        //给setmealDto赋值setmealDishes
        LambdaQueryWrapper<SetmealDish> sdlqw = new LambdaQueryWrapper<>();
        sdlqw.eq(SetmealDish::getSetmealId,id);
        setmealDto.setSetmealDishes(setmealDishService.list(sdlqw));

        return R.success(setmealDto);
    }


}
